package com.svms.service.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.svms.service.dml.iface.SpareDMLIF;
import com.svms.service.dml.iface.VehicleDMLIF;
import com.svms.service.factory.SvmsServiceFactory;
import com.svms.service.iface.AlertIF;
import com.svms.service.util.AlertComparitor;
import com.svms.service.vo.AlertVO;
import com.svms.service.vo.VehicleAlertVO;
import com.svms.service.vo.VehicleVO;

public class AlertBO implements AlertIF {

	VehicleDMLIF vehicleDML;

	public VehicleDMLIF getVehicleDML() {
		return vehicleDML;
	}

	public void setVehicleDML(VehicleDMLIF vehicleDML) {
		this.vehicleDML = vehicleDML;
	}

	SpareDMLIF SpareDML;

	/**
	 * Returns both vehicle and spares alerts
	 * 
	 * @return
	 */
	public List<AlertVO> getAllAlerts() {
		List<AlertVO> allAlerts = new ArrayList<AlertVO>();
		// List<AlertVO> allAlertsNew = new ArrayList<AlertVO>(5);
		List<AlertVO> vehicleAlerts = SvmsServiceFactory.getInstance()
				.getAlert().getAllVehicleAlerts();
		allAlerts.addAll(vehicleAlerts);
		return allAlerts.subList(0, 10);
	}

	/**
	 * Returns vehicle alerts alone
	 * 
	 * @return
	 */
	public List<AlertVO> getAllVehicleAlerts() {
		List<VehicleVO> vehiclesList = getVehicleDML().getVehicleList();
		List<AlertVO> alerts = new ArrayList<AlertVO>();
		for (VehicleVO vehicleVO : vehiclesList) {
			Map<String, Date> vehcileDocs = vehicleVO.getVehicleDocuments();
			for (String itemID : vehcileDocs.keySet()) {
				VehicleAlertVO alertVO = new VehicleAlertVO();
				alertVO.setVehicleNumber(vehicleVO.getVehicleNumber());
				alertVO.setItemID(itemID);
				alertVO.setDueDate(vehcileDocs.get(itemID));
				alerts.add(alertVO);
			}
		}
		Collections.sort(alerts, new AlertComparitor());
		return alerts;
	}

	public List<AlertVO> getAllSpareAlerts() {
		return null;
	}

}
